package org.apnprovider.ws;
import java.io.BufferedReader;
import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * Utility filter to dump incoming POST requests 
 * 
 * author Alexey Palamarchuk (slonnik@gmail.com)
 *
 */
public class PostDataDumperFilter implements Filter{

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
	
		BufferedReader reader = request.getReader();
		StringBuilder output=new StringBuilder();
		String buffer = reader.readLine();
		while(buffer != null)
		{
			output.append(buffer);
			buffer = reader.readLine();
		}
		reader.close();
		request.setAttribute("dumpdata", output); 
		chain.doFilter(request, response); 
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}
}
